class AchievementItemUI extends eui.ItemRenderer{
	private oldData:any;
	private lb_condition:eui.Label;
	private img_icon:eui.Image;
	private lb_num:eui.Label;
	private lb_progress:eui.Label;
	private btn_get:BaseButton;
	private img_progress:eui.Image;
	public constructor() {
		super();
		this.skinName = 'AchievementItemView';
	}

	private onRemoveToStageHandler(e:egret.Event){
		this.clear();
	}

	protected childrenCreated()
    {
        super.childrenCreated();
        this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.onRemoveToStageHandler, this);
        this.btn_get.addTapHandler(this.getAward, this);
    }

	public getAward()
	{
        if(this.data.Info.curState == 1)
        {
            //已领取
            MessageUtil.tips('已领取');
        }
        else if(this.data.Info.curState == 2)
        {
            //未完成
            MessageUtil.tips('未完成');
        }
        else
        {
            //可领取
            this.getReward();
        }
	}

    public getReward()
    {
        let id = this.data.id;
        let keyword = this.data.keyword;
        let sendData = {
			version:GameConst.version,
            id:id,
            keyword:keyword
		}
        let self = this;
		HttpManager.instance().sendPost(GameConst.httpUrl + 'warrioruser/get_achieve_award' , StringUtil.jsonToHttpPostString(sendData), (e)=>{
			let data = JSON.parse(e);
			if(data && data.msg == 'ok')
			{
                MessageUtil.tips('领取成功');
                let achieve = data.data.achieve;
                if(achieve != RoleCache.instance().isHaveAchievement)
                {
                    RoleCache.instance().isHaveAchievement = achieve;
                    GameUtil.updateRedPoint(RedPointID.Activity);
                }
                self.data.Info.curState = 1;
                self.updateUI();
                let award = data.data.award;
                GameUtil.openAwardUI(award,null,null);
			}
		})
    }

	public setMask(data)
    {
        let getShorten = function(num)
        {
            let str = '';
            if(num >= 10000)
            {
                let i = (num / 10000).toFixed(1);
                str = i + 'w';
                return str;
            }
            if(num >= 1000)
            {
                let i = (num / 1000).toFixed(1);
                str = i + 'k';
                return str;
            }
            str = num+'';
            return str;
        }

        let haveCnt = this.data.Info.curNum;
        let totalNum = data.num;
        haveCnt = haveCnt >= totalNum? totalNum: haveCnt;
        this.lb_progress.text = getShorten(haveCnt) + '/' + getShorten(totalNum);
        let progress = haveCnt / totalNum;
        
        let barMask = new egret.Rectangle(0, 0, progress * this.img_progress.width, this.img_progress.height);
        this.img_progress.mask = barMask;
    }

	public init()
	{
        if(!this.data)return;
        
        this.updateUI();
	}

	public updateUI()
	{
        let data = this.data;
        let condition = data.condition;
        let condition_data = ConditionUtil.instance().parseConditon(condition);
        this.lb_condition.text = condition_data.desc;
        let award = ResourceUtil.instance().parseSignleAward(this.data.award);
        this.img_icon.source = award.itemIcon;
        this.lb_num.text = award.itemNum + '';
        let img_get = this.btn_get.getChildByName('img_get') as eui.Image;
        if(this.data.Info.curState == 1 || this.data.Info.curState == 3)
        {   
            this.data.Info.curNum = condition_data.num;
        }
        if(this.data.Info.curState == 1 || this.data.Info.curState == 2)
        {
            ShaderUtil.ImgGray(img_get);
            if(this.data.Info.curState == 1)
            {
                img_get.source = 'achievement_5';
            }
            else
            {
                img_get.source = 'achievement_6';
            }
        }
        else
        {
            ShaderUtil.ImgDefault(img_get);
        }
        this.setMask(condition_data);
	}

	public dataChanged()
	{
		if(this.data == this.oldData)return;
        this.oldData = this.data;
        this.init();
	}

	public clear()
    {
        this.data = null;
        this.oldData = null;
        this.removeEventListener(egret.Event.REMOVED_FROM_STAGE, this.onRemoveToStageHandler,this);
		this.btn_get.removeTapHandler();
    }
}